//
//  HOMNSArrayCollectTests.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 09/10/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "HOMNSArrayCollectTests.h"


@implementation HOMNSArrayCollectTests

- (id)init {
	if ((self = [super init])) {
		int i;
		
		msg = [MSG(stringByAppendingString:@"x") retain];
		
		for (i = 0; i < 10000; i++)
			[(NSMutableArray *)largeArrayResult addObject:[NSString stringWithFormat:@"%dx", i]];
	}
	
	return self;
}

- (void)dealloc {
	[msg release]; msg = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Small Array Tests

- (id)testSmallArray {
	return [smallArray collect:MSG(stringByAppendingString:@"x")];
}

- (id)testSmallArray_CachedMessage {
	return [smallArray collect:msg];
}

- (id)testSmallArray_objectAtIndex {
	unsigned i;
	NSMutableArray *r = [NSMutableArray array];
	unsigned c = [smallArray count];
	
	for (i = 0U; i < c; i++)
		[r addObject:[[smallArray objectAtIndex:i] stringByAppendingString:@"x"]];
	
	return r;
}

- (id)testSmallArray_NSEnumerator {
	NSMutableArray *r = [NSMutableArray array];
	NSEnumerator *e = [smallArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject]))
		[r addObject:[str stringByAppendingString:@"x"]];
	
	return r;
}

#pragma mark -
#pragma mark Large Array Tests

- (id)testLargeArray {
	return [largeArray collect:MSG(stringByAppendingString:@"x")];
}

- (id)testLargeArray_CachedMessage {
	return [largeArray collect:msg];
}

- (id)testLargeArray_objectAtIndex {
	unsigned i;
	NSMutableArray *r = [NSMutableArray array];
	unsigned c = [largeArray count];
	
	for (i = 0U; i < c; i++)
		[r addObject:[[largeArray objectAtIndex:i] stringByAppendingString:@"x"]];
	
	return r;
}

- (id)testLargeArray_NSEnumerator {
	NSMutableArray *r = [NSMutableArray array];
	NSEnumerator *e = [largeArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject]))
		[r addObject:[str stringByAppendingString:@"x"]];
	
	return r;
}

#pragma mark -
#pragma mark Tests Verification

- (BOOL)verifyResult:(NSArray *)arr ofTest:(SEL)sel {
	NSString *name = NSStringFromSelector(sel);
	
	if ([name hasPrefix:@"testSmallArray"]) {
		DPTestAssert([arr count] == 3U, @"The result doesn't contain 3 objects. Result = %@", arr);
		DPTestAssert([arr containsObject:@"ax"], @"The result doesn't contain 'ax'. Result = %@", arr);
		DPTestAssert([arr containsObject:@"abcx"], @"The result doesn't contain 'abcx'. Result = %@", arr);
		DPTestAssert([arr containsObject:@"bx"], @"The result doesn't contain 'bx'. Result = %@", arr);
	}
	
	return [super verifyResult:arr ofTest:sel];
}

@end
